home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / shoot / MergeScores.lha / MergeScores / MergeScores.c < prev    next >
C/C++ Source or Header  |  1995-02-07  |  3KB  |  162 lines

  1. /*
  2.  *  MergeScores.c    Merges Megaball score tables.
  3.  *  (C) 1995 Richard Gledhill  -  93rpg@club.eng.cam.ac.uk
  4.  *  Compile with Dice with "dcc mergescores.c -o MergeScores"
  5.  *  7/2/95
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. #define FALSE 0
  12. #define TRUE  -1
  13. #define DEBUG FALSE
  14.  
  15. int
  16. brk()
  17. {
  18.     puts("Stopped.");
  19.     return(1);
  20. }
  21.  
  22. main(ac, av)
  23. char *av[];
  24. {
  25.     int   i, j;
  26.     long  score1 = 0, score2 = 0;
  27.   unsigned char  table1[24], table2[24], table3[24],                                        /* Score buffer  */
  28.        *tablename1 = av[1], *tablename2 = av[2], *tablename3 = av[3],        /* Tablenames    */
  29.         name1[21],  name2[21];                                                                                    /* Players names */
  30.   FILE *ft1 = NULL, *ft2 = NULL, *ft3 = NULL;                                                        /* File pointers */
  31.   unsigned char  rt1 = TRUE, rt2 = TRUE;
  32.  
  33.   if (ac < 4)
  34.     {
  35.         printf("Megaball High Score Table-merging utility.\n");
  36.         printf("(C) 1995 Richard Gledhill.\n");
  37.         printf("Usage:\n     %s <table1> <table2> <outputtable>\n",av[0]);
  38.         exit(1);
  39.     }
  40.  
  41.     onbreak(brk);
  42.  
  43.     name1[20] = 0;
  44.     name2[20] = 0;
  45.  
  46. /*
  47.  * Open table 1
  48.  */
  49.  
  50.     if (!(ft1 = fopen(tablename1,"rb")))
  51.     {
  52.         printf("Can't open table 1 : `%s' .\n",tablename1);
  53.         return(1);
  54.     }
  55.     
  56. /*
  57.  * Open table 2
  58.  */
  59.  
  60.     if (!(ft2 = fopen(tablename2,"rb")))
  61.     {
  62.         printf("Can't open table 2 : `%s' .\n",tablename2);
  63.         return(1);
  64.     }
  65.  
  66. /*
  67.  * Open output table (3)
  68.  */
  69.     
  70.     if (!(ft3 = fopen(tablename3,"wb")))
  71.     {
  72.         printf("Can't open table 3 : `%s' .\n",tablename3);
  73.         return(1);
  74.     }
  75.  
  76.     printf("New Score Table:\n");
  77.  
  78.     for (i = 0; i < 15; i++)
  79.     {
  80.         /* Read in score from table 1 if needed */
  81.         if (rt1)
  82.         {
  83.             fread(table1,24,1,ft1);
  84. #if DEBUG
  85.             for (j = 0; j < 24; j++) printf("%02i ",table1[j]);
  86.             printf("\n");
  87. #endif
  88.             strncpy(name1,table1,20);
  89.             score1 = 0;
  90.             for (j = 20; j < 24; j++)  score1 = score1*256 + table1[j];
  91.         }
  92.         
  93.         /* Read in score from table 2 if needed */
  94.         if (rt2)
  95.         {
  96.             fread(table2,24,1,ft2);
  97.             strncpy(name2,table2,20);
  98. #if DEBUG
  99.             for (j = 0; j < 24; j++) printf("%02i ",table2[j]);
  100.             printf("\n");
  101. #endif
  102.             score2 = 0;
  103.             for (j = 20; j < 24; j++)  score2 = score2*256 + table2[j];
  104.         }
  105.         
  106. #if DEBUG
  107.         printf("Name1: %s    Score: %li\n",name1,score1);
  108.         printf("Name2: %s    Score: %li\n",name2,score2);
  109. #endif
  110.  
  111.         if ((strcmp(name1,name2) == 0) && (score1 == score2))
  112.         {
  113.           /* Strip out duplicate entries: only write out one. */
  114.             fwrite(table1,24,1,ft3);
  115.             fwrite(table1,20,1,stdout);
  116.             printf(" %8ld\n",score1);
  117.             rt1 = TRUE;
  118.             rt2 = TRUE;
  119.         }
  120.         else if (score1 > score2)
  121.         {
  122.             /* Write out name and score from table 1 */
  123.             fwrite(table1,24,1,ft3);
  124.             fwrite(table1,20,1,stdout);
  125.             printf(" %8ld\n",score1);
  126.             rt1 = TRUE;
  127.             rt2 = FALSE;
  128.         }
  129.         else if (score1 < score2)
  130.         {
  131.             /* Write out name and score from table 2 */
  132.             fwrite(table2,24,1,ft3);
  133.             fwrite(table2,20,1,stdout);
  134.             printf(" %8ld\n",score2);
  135.             rt1 = FALSE;
  136.             rt2 = TRUE;
  137.         }
  138.         else
  139.         {
  140.             /* Write out name and score from both tables */
  141.             fwrite(table1,24,1,ft3);
  142.             fwrite(table1,20,1,stdout);
  143.             printf(" %8ld\n",score1);
  144.             if (i < 14)
  145.             {
  146.                 fwrite(table2,24,1,ft3);
  147.                 fwrite(table2,20,1,stdout);
  148.                 printf(" %8ld\n",score2);
  149.             }
  150.             rt1 = TRUE;
  151.             rt2 = TRUE;
  152.             i++;
  153.         }
  154.   }        
  155.  
  156.     fclose(ft1);
  157.     fclose(ft2);
  158.     fclose(ft3);
  159.   return(0);
  160. }
  161.  
  162.